package com.jzr.bedside.ui.voip

import android.os.SystemClock
import android.view.View
import android.widget.Chronometer
import com.blankj.utilcode.utils.ToastUtils
import com.jzr.bedside.R
import com.jzr.bedside.base.BaseActivity
import com.jzr.bedside.base.Constant
import com.jzr.bedside.utils.rtc.AEvent
import com.jzr.bedside.utils.rtc.IEventListener
import com.starrtc.starrtcsdk.api.XHClient
import com.starrtc.starrtcsdk.api.XHConstants
import com.starrtc.starrtcsdk.api.XHVoipManager
import com.starrtc.starrtcsdk.apiInterface.IXHResultCallback
import com.starrtc.starrtcsdk.core.audio.StarRTCAudioManager
import com.starrtc.starrtcsdk.core.pusher.XHCameraRecorder
import kotlinx.android.synthetic.main.activity_voip_audio.*

class VoipAudioActivity : BaseActivity(), IEventListener, View.OnClickListener {

    private var starRTCAudioManager: StarRTCAudioManager? = null

    private var voipManager: XHVoipManager? = null
    private val timer: Chronometer? = null

    companion object {
        var ACTION = "ACTION"
        var RING = "RING"
        var CALLING = "CALLING"
    }

    private var action: String? = null
    private var targetId: String? = null


    override fun getLayoutId(): Int {
        return R.layout.activity_voip_audio
    }

    override fun attachView() {

    }

    override fun detachView() {

    }


    override fun initView() {
        hangup.setOnClickListener(this)
        starRTCAudioManager =
            StarRTCAudioManager.create(this.applicationContext)
        starRTCAudioManager?.start { selectedAudioDevice, availableAudioDevices ->

        }

        voipManager = XHClient.getInstance().voipManager
        voipManager?.rtcMediaType = XHConstants.XHRtcMediaTypeEnum.STAR_RTC_MEDIA_TYPE_AUDIO_ONLY
        voipManager?.setRecorder(XHCameraRecorder())
        addListener()

        targetId = intent.getStringExtra("targetId")
        action = intent.getStringExtra(ACTION)

        if (action == CALLING) {
            showCallingView()
            voipManager?.audioCall(
                this,
                targetId,
                object : IXHResultCallback {
                    override fun success(data: Any?) {
                    }

                    override fun failed(errMsg: String?) {
                        stopAndFinish()
                    }
                })
        } else {
            onPickup()
        }

    }

    override fun onDestroy() {
        removeListener()
        super.onDestroy()
    }

    private fun stopAndFinish() {
        if (starRTCAudioManager != null) {
            starRTCAudioManager?.stop()
        }
        finish()
    }

    private fun showCallingView() {
        findViewById(R.id.calling_txt).visibility = View.VISIBLE
        findViewById(R.id.timer).visibility = View.INVISIBLE
    }

    private fun showTalkingView() {
        findViewById(R.id.calling_txt).visibility = View.INVISIBLE
        findViewById(R.id.timer).visibility = View.VISIBLE
        timer?.base = SystemClock.elapsedRealtime()
        timer?.start()
        setupView()
    }

    private fun setupView() {
        voipManager?.setupView(
            null,
            null,
            object : IXHResultCallback {
                override fun success(data: Any?) {
                }

                override fun failed(errMsg: String?) {
                    stopAndFinish()
                }
            })
    }

    private fun onPickup() {
        voipManager?.accept(
            this,
            targetId,
            object : IXHResultCallback {
                override fun success(data: Any?) {
                }
                override fun failed(errMsg: String?) {
                    stopAndFinish()
                }
            })
        showTalkingView()
    }

    fun addListener() {
        AEvent.addListener(AEvent.AEVENT_VOIP_INIT_COMPLETE, this)
        AEvent.addListener(AEvent.AEVENT_VOIP_REV_BUSY, this)
        AEvent.addListener(AEvent.AEVENT_VOIP_REV_REFUSED, this)
        AEvent.addListener(AEvent.AEVENT_VOIP_REV_HANGUP, this)
        AEvent.addListener(AEvent.AEVENT_VOIP_REV_CONNECT, this)
        AEvent.addListener(AEvent.AEVENT_VOIP_REV_ERROR, this)
    }

    fun removeListener() {
        Constant.canPickupVoip = true
        AEvent.removeListener(AEvent.AEVENT_VOIP_INIT_COMPLETE, this)
        AEvent.removeListener(AEvent.AEVENT_VOIP_REV_BUSY, this)
        AEvent.removeListener(AEvent.AEVENT_VOIP_REV_REFUSED, this)
        AEvent.removeListener(AEvent.AEVENT_VOIP_REV_HANGUP, this)
        AEvent.removeListener(AEvent.AEVENT_VOIP_REV_CONNECT, this)
        AEvent.removeListener(AEvent.AEVENT_VOIP_REV_ERROR, this)
    }

    override fun dispatchEvent(aEventID: String?, success: Boolean, eventObj: Any?) {
        when (aEventID) {
            AEvent.AEVENT_VOIP_REV_BUSY -> {
                ToastUtils.showShortToast("对方线路忙")
                stopAndFinish()
            }
            AEvent.AEVENT_VOIP_REV_REFUSED -> {
                ToastUtils.showShortToast("对方拒绝通话")
                stopAndFinish()
            }
            AEvent.AEVENT_VOIP_REV_HANGUP -> {
                ToastUtils.showShortToast("对方已挂断")
                timer?.stop()
                stopAndFinish()
            }
            AEvent.AEVENT_VOIP_REV_CONNECT -> {
                ToastUtils.showShortToast("对方允许通话")
                showTalkingView()
            }
            AEvent.AEVENT_VOIP_REV_ERROR -> {
                ToastUtils.showShortToast(eventObj as String?)
                stopAndFinish()
            }
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.hangup -> {
                stopAndFinish()
                voipManager!!.hangup(object : IXHResultCallback {
                    override fun success(data: Any?) {
                        stopAndFinish()
                    }

                    override fun failed(errMsg: String?) {
                        stopAndFinish()
                    }
                })
            }
        }
    }
}
